package org.meetunexpectedly.util;

import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author: pupil
 * @Date: 2024/08/05 下午 6:26
 */
@Service(value = "captchaUtil")
public class CaptchaUtil {

    @Autowired
    private Producer producer;

    /**
     * 获取图形验证码
     * @return
     */
    public Map<Object,Object> getCaptcha(){
        // 获取验证码
        String code = producer.createText();
        // 验证码转换为图形
        BufferedImage image = producer.createImage(code);
        Map<Object, Object> map = new HashMap<>();
        map.put(String.class, code);
        map.put(BufferedImage.class, image);
        return map;
    }

    /**
     * 获取邮箱验证码
     *
     * @return
     */
    public String getCode() {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            int code = (int) (Math.random()*10);
            builder.append(code);
        }
        return builder.toString();
    }
}
